package com.babystudy.utils;


import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.RectangleReadOnly;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;


public class MathGenerator {
	private static Document document;
	private static Font fonttitle;
	private static PdfPTable table = null;
    public static void main(String garg[]) {
    	
    	document= new Document(new RectangleReadOnly(842F, 595F));
    	// System.out.println("========================================");  
    	 document.setPageSize(PageSize.A4.rotate());
    		document.setMargins(10, 10, 10, 10);
    	//	do20(document,50,100);
    		document.open();
    		Multiplydo20(document,50,100);    
    		document.close();
    }
	
	
	 public static void do20(Document document,int rounds,int limit) {
		
		
		 table = new PdfPTable(2); 
	
			fonttitle = new Font(Font.getFamilyIndex("HELVETICA"), 18, Font.NORMAL);
		 try {
			 document.add(new Paragraph("Date:____________ ", fonttitle));
				document.add(new Paragraph("Time:______________      Acc:______", fonttitle));
				document.add(new Paragraph(" ", fonttitle));
			
		 for(int i =1;i<=rounds;i++) {
			 if(i%51 == 0) {
				 document.add(table);
				 table = new PdfPTable(2); 
				 document.newPage();
				//System.out.println("new page------");
					try {
						document.add(new Paragraph("Date:____________ ", fonttitle));
						document.add(new Paragraph("Time:______________      Acc:______", fonttitle));
						document.add(new Paragraph("  ", fonttitle));
						
					} catch (DocumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			 }
		 int a = (int)(Math.random()*limit);
		 int b = (int)(Math.random()*(limit - a));
		 int op = (int)(Math.random()*2); //������
		 if(op == 1) {
		//	System.out.println(a+" + "+b +" = ");
			 table.addCell(new Paragraph(a+" + "+b +" = ",fonttitle));
		 }
		 else 
				 if(a>b)
				 {
			//		 System.out.println(a+" - "+b+" = ");
					 table.addCell(new Paragraph(a+" - "+b+" = ",fonttitle));
				 }
				 else {
			//		System.out.println(b+" - "+a+" = ");
					 table.addCell(new Paragraph(b+" - "+a+" = ",fonttitle));
				 }
			 }
		 
		 
			document.add(table);
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		 
			//Step 5��Close the Document.  
			document.close();
			
	 }
	
	 public static void Multiplydo20(Document document,int rounds,int limit) {
			
			
		 table = new PdfPTable(2); 
	
			fonttitle = new Font(Font.getFamilyIndex("HELVETICA"), 18, Font.NORMAL);
		 try {
			 document.add(new Paragraph("Date:____________ ", fonttitle));
				document.add(new Paragraph("Time:______________      Acc:______", fonttitle));
				document.add(new Paragraph(" ", fonttitle));
			
		 for(int i =0;i<rounds;i++) {
			 if(i%50 == 0) {
				 // 51,101,151,201,251
				 document.add(table);
				 table = new PdfPTable(2); 
				 document.newPage();
				//System.out.println("new page------");
					try {
						document.add(new Paragraph("Date:____________ ", fonttitle));
						document.add(new Paragraph("Time:______________      Acc:______", fonttitle));
						document.add(new Paragraph("  ", fonttitle));
						
					} catch (DocumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			 }
			 int numpy1 = (int)(Math.random()*8)+2;
			 int numpy2 = (int)(Math.random()*8)+2;
			 table.addCell(new Paragraph(numpy1+" x "+numpy2+" = ",fonttitle));
	
			 }
		 
		 
			document.add(table);
			
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		 
			//Step 5��Close the Document.  
			document.close();
			
	 }

}
